package com.wan37.logic.backpack.item;

import com.wan37.logic.dao.UserItemDao;
import com.wan37.logic.db.AsynDBManager;
import com.wan37.logic.domain.entity.UserItemDO;
import com.wan37.logic.domain.entity.UserWeaponryDO;
import com.wan37.logic.domain.vo.ItemVO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.RoleVO;
import com.wan37.logic.service.UserItemService;
import com.wan37.logic.service.cache.ItemServiceCache;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.CommonBackpack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.Column;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-17 10:00
 **/
@Component
public class ItemManager {
    @Autowired
    private AsynDBManager asynDBManager;
    @Autowired
    private UserItemService userItemService;
    @Autowired
    private ItemServiceCache itemServiceCache;
    @Autowired
    private RouteUtils routeUtils;

    public boolean addItem(UserServiceCache.User user, String itemId, int count) {
        if (user.getItemVOMap().containsKey(itemId)) {
            user.getItemVOMap().get(itemId).setCount(user.getItemVOMap().get(itemId).getCount() + count);
            UserItemDO userItemDO = new UserItemDO(user.getItemVOMap().get(itemId), user.getUserVO().getUserId());
            asynDBManager.distributeWork(user.getUserVO().getUserId(), () -> userItemService.update(userItemDO));
        } else {
            ItemVO itemVO = itemServiceCache.getItemServiceMap().get(itemId).getItemVO();
            ItemVO newItemVO = new ItemVO(itemVO);
            user.getItemVOMap().put(itemId, newItemVO);
            UserItemDO userItemDO = new UserItemDO(newItemVO, user.getUserVO().getUserId());
            asynDBManager.distributeWork(user.getUserVO().getUserId(), () -> userItemService.save(userItemDO));
        }
        MessageVO messageVO = new MessageVO(user.getUserVO().getUserId());
        routeUtils.sendItemResponse(CommonBackpack.ITEM_SUCCESS,
                messageVO.getUserId() + "物品信息获取成功！", user.getItemVOMap(), messageVO);
        return true;
    }

    public boolean reduceItem(UserServiceCache.User user, String itemId, int count) {
        ItemVO itemVO = user.getItemVOMap().get(itemId);
        itemVO.setCount(itemVO.getCount() - count);
        String userId = user.getUserVO().getUserId();
        UserItemDO userItemDO = new UserItemDO(itemVO, user.getUserVO().getUserId());
        asynDBManager.distributeWork(userId, () -> userItemService.update(userItemDO));
        MessageVO messageVO = new MessageVO(user.getUserVO().getUserId());
        routeUtils.sendItemResponse(CommonBackpack.ITEM_SUCCESS,
                messageVO.getUserId() + "物品信息获取成功！", user.getItemVOMap(), messageVO);
        return true;
    }

    public boolean reduceItem(UserServiceCache.User user, ItemVO itemVO, int count) {
        itemVO.setCount(itemVO.getCount() - count);
        String userId = user.getUserVO().getUserId();
        UserItemDO userItemDO = new UserItemDO(itemVO, user.getUserVO().getUserId());
        asynDBManager.distributeWork(userId, () -> userItemService.update(userItemDO));
        MessageVO messageVO = new MessageVO(user.getUserVO().getUserId());
        routeUtils.sendItemResponse(CommonBackpack.ITEM_SUCCESS,
                messageVO.getUserId() + "物品信息获取成功！", user.getItemVOMap(), messageVO);
        return true;
    }
}
